from data import parkings as data


def create_parking(
    address: str, opened: bool, count_places: int, count_available_places: int
) -> dict:
    return data.create_parking_json(
        address, opened, count_places, count_available_places
    )


def is_parking_available(parking_id: int) -> bool:
    parking = data.get_parking_json(parking_id)
    return parking["opened"] and parking["count_available_places"] > 0


def decrease_places_count(parking_id: int) -> dict:
    parking = data.get_parking_json(parking_id)
    return data.update_parking_json(
        parking_id,
        address=parking["address"],
        opened=parking["opened"],
        count_places=parking["count_places"],
        count_available_places=(
            parking["count_available_places"] - 1
            if parking["count_available_places"] > 0
            else 0
        ),
    )


def increase_places_count(parking_id: int) -> dict:
    parking = data.get_parking_json(parking_id)
    return data.update_parking_json(
        parking_id,
        address=parking["address"],
        opened=parking["opened"],
        count_places=parking["count_places"],
        count_available_places=(
            parking["count_available_places"] + 1
            if parking["count_available_places"] < parking["count_places"]
            else parking["count_places"]
        ),
    )
